package com.hjn.json;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class AnalyiseJsonJDK7 {
    public static void main(String[] args) throws IOException {
        String path = "E:\\study\\jcode\\son\\src\\main\\doc\\json.txt";
        File jsonFile = new File(path);
        StringBuffer jsonStr = new StringBuffer("");
        InputStream inputStream = new FileInputStream(jsonFile);
        int len = 0;
        byte array[] = new byte[1024];
        while ((len = inputStream.read(array)) != -1) {
            String s = new String(array, 0, len, "gbk");
            jsonStr.append(s);
        }
        inputStream.close();
        //将返回值转成jsonobject对象
        JSONArray jsonArray = JSONArray.parseArray(jsonStr.toString());
        JSONObject jsonObj = (JSONObject) jsonArray.get(0);

        //创建四个对象集合
        List<JSONObject> step1 = new ArrayList();
        List<JSONObject> step2 = new ArrayList();
        List<JSONObject> step3 = new ArrayList();
        List<JSONObject> step4 = new ArrayList();

        //遍历键值
        Set<String> keySet = jsonObj.keySet();
        //长度-4 就是分组的个数 每个对象有6个属性
        System.out.println("属性个数keySet = " + keySet.size());
        for (String key : keySet) {
            String value = (String) jsonObj.get(key);
            System.out.println(key + "value = " + value);
            JSONObject obj = new JSONObject();
            obj.put(key, value);
            if(key.contains("step1")){
                step1.add(obj);
            }else if(key.contains("step2")){
                step2.add(obj);
            }else if(key.contains("step3")){
                step3.add(obj);
            }else if(key.contains("step4")){
                step4.add(obj);
            }
        }
        System.out.println("==================================================================");
        System.out.println("办理人" + "\t" + "办理时间"+ "\t" +"办理环节"+ "\t" +"办理状态"+ "\t" +"备注");
        for (JSONObject jsonObject : step1) {
            Set<String> step1Keys = jsonObject.keySet();
            for (String key : step1Keys) {
                String value = (String) jsonObj.get(key);
                System.out.print(key +"====="+value);
            }
        }
    }
}
